package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjNWarningLqBo;
import com.ruoyi.zhgd.domain.vo.ZjNWarningLqVo;
import com.ruoyi.zhgd.domain.ZjNWarningLq;
import com.ruoyi.zhgd.mapper.ZjNWarningLqMapper;
import com.ruoyi.zhgd.service.IZjNWarningLqService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青拌合站预警信息Service业务层处理
 *
 * @author tll
 * @date 2024-09-27
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjNWarningLqServiceImpl implements IZjNWarningLqService {

    private final ZjNWarningLqMapper baseMapper;

    /**
     * 查询沥青拌合站预警信息
     */
    @Override
    public ZjNWarningLqVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站预警信息列表
     */
    @Override
    public TableDataInfo<ZjNWarningLqVo> queryPageList(ZjNWarningLqBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjNWarningLq> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(ZjNWarningLq::getInsterTime);
        Page<ZjNWarningLqVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站预警信息列表
     */
    @Override
    public List<ZjNWarningLqVo> queryList(ZjNWarningLqBo bo) {
        LambdaQueryWrapper<ZjNWarningLq> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjNWarningLq> buildQueryWrapper(ZjNWarningLqBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjNWarningLq> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZjNWarningLq::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getGrade()), ZjNWarningLq::getGrade, bo.getGrade());
        lqw.eq(bo.getInsterTime() != null, ZjNWarningLq::getInsterTime, bo.getInsterTime());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessId()), ZjNWarningLq::getBusinessId, bo.getBusinessId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ZjNWarningLq::getStatus, bo.getStatus());
        lqw.eq(bo.getDisposalStatus() != null, ZjNWarningLq::getDisposalStatus, bo.getDisposalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), ZjNWarningLq::getStationCode, bo.getStationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptCode()), ZjNWarningLq::getDeptCode, bo.getDeptCode());
        lqw.eq(StringUtils.isNotBlank(bo.getBuzTime()), ZjNWarningLq::getBuzTime, bo.getBuzTime());
        lqw.like(StringUtils.isNotBlank(bo.getMatchName()), ZjNWarningLq::getMatchName, bo.getMatchName());
        lqw.eq(StringUtils.isNotBlank(bo.getWarnItem()), ZjNWarningLq::getWarnItem, bo.getWarnItem());
        lqw.eq(StringUtils.isNotBlank(bo.getOutRange()), ZjNWarningLq::getOutRange, bo.getOutRange());
        lqw.eq(StringUtils.isNotBlank(bo.getCz()), ZjNWarningLq::getCz, bo.getCz());
        if (StringUtils.isNotBlank(bo.getStartTime()) && StringUtils.isNotBlank(bo.getEndTime())) {
            lqw.ge(ZjNWarningLq::getInsterTime, bo.getStartTime());
            lqw.le(ZjNWarningLq::getInsterTime, bo.getEndTime());
        }
        return lqw;
    }

    /**
     * 新增沥青拌合站预警信息
     */
    @Override
    public Boolean insertByBo(ZjNWarningLqBo bo) {
        ZjNWarningLq add = BeanUtil.toBean(bo, ZjNWarningLq.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站预警信息
     */
    @Override
    public Boolean updateByBo(ZjNWarningLqBo bo) {
        ZjNWarningLq update = BeanUtil.toBean(bo, ZjNWarningLq.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjNWarningLq entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站预警信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
